package com.zhang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zhangshihao
 * @create 2023-02-28 9:19
 */
// 如果，想要diy一些定制化功能，只需要写这个组件，交给springboot，springboot就会帮我们自动装配
// 扩展springmvc
@Configuration
// @EnableWebMvc // 导入了一个类：DelegatingWebMvcConfiguration：从容器中获取所有的webmvcconfig
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
    }

    // 自定义的国际化组件就生效了
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/index.html", "/", "/user/login", "/css/**", "/js/**", "/img/**");
    }

    // public interface ViewResolver 实现了视图解析器接口的类，我们就可以把它看做视图解析器
    // @Bean
    // public ViewResolver myViewResolver(){
    //     return new MyViewResolver();
    // }
    //
    // public static class MyViewResolver implements ViewResolver{
    //     // 自定义了自己的视图解析器
    //     @Override
    //     public View resolveViewName(String viewName, Locale locale) throws Exception {
    //         return null;
    //     }
    // }

    // 视图跳转
    // @Override
    // public void addViewControllers(ViewControllerRegistry registry) {
    //     registry.addViewController("/zhang").setViewName("test");
    // }
}